﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace DBProvider.Providers
{
	public class ActivityProvider : IActivityProvider
	{
		#region IActivityProvider Members

		public Activity CreateActivity(Activity theNewActivity, out string msg)
		{
			if(theNewActivity == null)
			{
				throw new ArgumentNullException();
			}

			msg = string.Empty;
			Activity result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.Activities.Count(ac => ac.ActivityTitle == theNewActivity.ActivityTitle) > 0;

					if(isExist)
					{
						msg = "同名的标题已存在";
					}
					else
					{
						db.Activities.InsertOnSubmit(theNewActivity);
						db.SubmitChanges();

						result = db.Activities.SingleOrDefault(ac => ac.ActivityTitle == theNewActivity.ActivityTitle);
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateActivity(Activity theActivity)
		{
			if(theActivity == null || theActivity.ActivityID <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbActivity = db.Activities.SingleOrDefault(ac => ac.ActivityID == theActivity.ActivityID);

					if(dbActivity != null)
					{
						dbActivity.Clone(theActivity);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteActivity(int theActivityID)
		{
			if(theActivityID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var tagSubmit = false;

					var dbRlt_UserAndActivity = db.RLT_ACTIVITYANDUSERs.Where(au => au.ActivityID == theActivityID);

					if(dbRlt_UserAndActivity != null)
					{
						db.RLT_ACTIVITYANDUSERs.DeleteAllOnSubmit(dbRlt_UserAndActivity);
						tagSubmit = true;
					}

					var dbRlt_ActivityAndShareTo = db.RLT_ActivityAndShareTos.Where(acs => acs.ActivityID == theActivityID);

					if(dbRlt_ActivityAndShareTo != null)
					{
						db.RLT_ActivityAndShareTos.DeleteAllOnSubmit(dbRlt_ActivityAndShareTo);
						tagSubmit = true;
					}

					var dbActivity = db.Activities.SingleOrDefault(ac => ac.ActivityID == theActivityID);

					if(dbActivity != null)
					{
						db.Activities.DeleteOnSubmit(dbActivity);
						tagSubmit = true;
					}

					if(tagSubmit)
					{
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public ActivityCategory CreateActivityCategory(ActivityCategory theNewActCat, out string msg)
		{
			if(theNewActCat == null)
			{
				throw new ArgumentNullException();
			}

			msg = string.Empty;
			ActivityCategory result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.ActivityCategories.Count(ac => ac.ActivityCategoryName == theNewActCat.ActivityCategoryName) > 0;

					if(isExist)
					{
						msg = "同名的名称已存在";
					}
					else
					{
						db.ActivityCategories.InsertOnSubmit(theNewActCat);

						db.SubmitChanges();

						result = db.ActivityCategories.SingleOrDefault(ac => ac.ActivityCategoryName == theNewActCat.ActivityCategoryName);
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateActivityCategory(ActivityCategory theActivityCategory)
		{
			if(theActivityCategory == null || theActivityCategory.ActivityCategoryID <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbActivityCat = db.ActivityCategories.SingleOrDefault(ac => ac.ActivityCategoryID == theActivityCategory.ActivityCategoryID);

					if(dbActivityCat != null)
					{
						dbActivityCat.ActivityCategoryDescription = theActivityCategory.ActivityCategoryDescription;
						dbActivityCat.ActivityCategoryName = theActivityCategory.ActivityCategoryName;

						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteActivityCategory(int theActCatID)
		{
			if(theActCatID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbActivityCat = db.ActivityCategories.SingleOrDefault(ac => ac.ActivityCategoryID == theActCatID);

					if(dbActivityCat != null)
					{
						db.ActivityCategories.DeleteOnSubmit(dbActivityCat);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<ActivityCategory> GetAllActivityCategory()
		{
			List<ActivityCategory> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = db.ActivityCategories;
					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetActivityByCategory(int theActCatID, int drugStoreID)
		{
			if(theActCatID <= 0 || drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = db.Activities.Where(ac => ac.ActivityCategoryID == theActCatID && ac.DrugStoreID == drugStoreID).OrderByDescending(a => a.DeadlineTime);
					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetActivityByCategory(int userID, int theActCatID, int drugStoreID)
		{
			if(theActCatID <= 0 || drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					var data = from act in db.Activities
										 where act.ActivityCategoryID == theActCatID && ((act.IsPublic == 1 && act.ActivityInitiator != userID) || ((from acs in db.RLT_ActivityAndShareTos
																																																																 where acs.ShareToDrugStoreID == user.DrugStoreID
																																																																 select acs.ActivityID).Contains(act.ActivityID)) || ((from acs in db.RLT_ActivityAndShareTos
																																																																																											 where acs.ShareToMemberID == userID
																																																																																											 select acs.ActivityID).Contains(act.ActivityID)))
										 orderby act.ActivityStartTime descending
										 select act;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetAllActivity(int drugStoreID)
		{
			if(drugStoreID <= 0)
			{
				throw new ArgumentException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = db.Activities.Where(ac => ac.DrugStoreID == drugStoreID).OrderByDescending(a => a.DeadlineTime);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetAllActivity(int drugStoreID, int userID)
		{
			if(drugStoreID <= 0 || userID <= 0)
			{
				throw new ArgumentException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					var data = from act in db.Activities
										 where (act.DrugStoreID == user.DrugStoreID) || (act.IsPublic == 1) || ((from acs in db.RLT_ActivityAndShareTos
																																														 where acs.ShareToDrugStoreID == user.DrugStoreID
																																														 select acs.ActivityID).Contains(act.ActivityID)) || ((from acs in db.RLT_ActivityAndShareTos
																																																																									 where acs.ShareToMemberID == userID
																																																																									 select acs.ActivityID).Contains(act.ActivityID))
										 orderby act.ActivityStartTime descending
										 select act;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public Activity GetActivity(int theActID)
		{
			if(theActID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			Activity result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Activity>(q => q.User);
					dlo.LoadWith<Activity>(q => q.RLT_ACTIVITYANDUSERs);
					db.LoadOptions = dlo;

					result = db.Activities.SingleOrDefault(ac => ac.ActivityID == theActID);
					if(result != null)
					{
						result.ActivityCategory = db.ActivityCategories.SingleOrDefault(acc => acc.ActivityCategoryID == result.ActivityCategoryID);
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetAllActivity(int drugStoreID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(drugStoreID <= 0)
			{
				throw new ArgumentException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					totalRecords = db.Activities.Where(ac => ac.DrugStoreID == drugStoreID).Count();

					var data = db.Activities.Where(ac => ac.DrugStoreID == drugStoreID).OrderByDescending(ac => ac.DeadlineTime).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetAllActivity(int userID, int drugStoreID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(drugStoreID <= 0 || userID <= 0)
			{
				throw new ArgumentException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					totalRecords = (from act in db.Activities
													where (act.DrugStoreID == user.DrugStoreID) || (act.IsPublic == 1) || ((from acs in db.RLT_ActivityAndShareTos
																																																	where acs.ShareToDrugStoreID == user.DrugStoreID
																																																	select acs.ActivityID).Contains(act.ActivityID)) || ((from acs in db.RLT_ActivityAndShareTos
																																																																												where acs.ShareToMemberID == userID
																																																																												select acs.ActivityID).Contains(act.ActivityID))
													select act).Count();

					var data = (from act in db.Activities
											where (act.DrugStoreID == user.DrugStoreID) || (act.IsPublic == 1) || ((from acs in db.RLT_ActivityAndShareTos
																																															where acs.ShareToDrugStoreID == user.DrugStoreID
																																															select acs.ActivityID).Contains(act.ActivityID)) || ((from acs in db.RLT_ActivityAndShareTos
																																																																										where acs.ShareToMemberID == userID
																																																																										select acs.ActivityID).Contains(act.ActivityID))
											orderby act.ActivityStartTime descending
											select act).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetActivityByCategory(int drugStoreID, int theActCatID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(theActCatID <= 0 || drugStoreID <= 0 || pageIndex < 0 || pageSize < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					totalRecords = db.Activities.Where(ac => ac.ActivityCategoryID == theActCatID && ac.DrugStoreID == drugStoreID).Count();

					var data = db.Activities.Where(ac => ac.ActivityCategoryID == theActCatID && ac.DrugStoreID == drugStoreID).OrderByDescending(a => a.DeadlineTime).Skip(pageIndex * pageSize).Take(pageSize);
					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetActivityByCategory(int userID, int drugStoreID, int theActCatID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(theActCatID <= 0 || drugStoreID <= 0 || pageIndex < 0 || pageSize < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					totalRecords = (from act in db.Activities
													where act.ActivityCategoryID == theActCatID && ((act.IsPublic == 1 && act.ActivityInitiator != userID) || ((from acs in db.RLT_ActivityAndShareTos
																																																																			where acs.ShareToDrugStoreID == user.DrugStoreID
																																																																			select acs.ActivityID).Contains(act.ActivityID)) || ((from acs in db.RLT_ActivityAndShareTos
																																																																																														where acs.ShareToMemberID == userID
																																																																																														select acs.ActivityID).Contains(act.ActivityID)))
													select act).Count();

					var data = (from act in db.Activities
											where act.ActivityCategoryID == theActCatID && ((act.IsPublic == 1 && act.ActivityInitiator != userID) || ((from acs in db.RLT_ActivityAndShareTos
																																																																	where acs.ShareToDrugStoreID == user.DrugStoreID
																																																																	select acs.ActivityID).Contains(act.ActivityID)) || ((from acs in db.RLT_ActivityAndShareTos
																																																																																												where acs.ShareToMemberID == userID
																																																																																												select acs.ActivityID).Contains(act.ActivityID)))
											orderby act.ActivityStartTime descending
											select act).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyInitiatActivity(int userID)
		{
			if(userID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = db.Activities.Where(ac => ac.ActivityInitiator == userID).OrderByDescending(a => a.DeadlineTime);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyRegistrationActivity(int userID)
		{
			if(userID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = from ac in db.Activities
										 where (from au in db.RLT_ACTIVITYANDUSERs
														where au.userID == userID
														select au.ActivityID).Contains(ac.ActivityID)
										 orderby ac.DeadlineTime descending
										 select ac;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool RegistrationActivity(int userID, int activityID)
		{
			if(userID <= 0 || activityID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.RLT_ACTIVITYANDUSERs.Count(au => au.ActivityID == activityID && au.userID == userID) > 0 &&
						db.Activities.Count(ac => ac.ActivityID == activityID) > 0 &&
						db.Users.Count(u => u.UserID == userID) > 0;

					if(!isExist)
					{
						var theNewActivityAndUser = new RLT_ACTIVITYANDUSER() { ActivityID = activityID, userID = userID };

						db.RLT_ACTIVITYANDUSERs.InsertOnSubmit(theNewActivityAndUser);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyInitiatActivity(int userID, int theActCatID)
		{
			if(userID <= 0 || theActCatID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = db.Activities.Where(ac => ac.ActivityInitiator == userID && ac.ActivityCategoryID == theActCatID).OrderByDescending(a => a.DeadlineTime);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyRegistrationActivity(int userID, int theActCatID)
		{
			if(userID <= 0 || theActCatID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = from ac in db.Activities
										 where ac.ActivityCategoryID == theActCatID &&
										 (from au in db.RLT_ACTIVITYANDUSERs
											where au.userID == userID
											select au.ActivityID).Contains(ac.ActivityID)
										 select ac;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetNewActivities(int count, int drugStoreID)
		{
			if(count <= 0 || drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Activity>(q => q.RLT_ACTIVITYANDUSERs);
					db.LoadOptions = dlo;

					var data = from ac in db.Activities
										 where ac.DrugStoreID == drugStoreID
										 orderby ac.ActivityStartTime descending
										 select ac;

					if(data != null)
					{
						result = data.Take(count).ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool CreateRLTActivityAndUser(RLT_ACTIVITYANDUSER theRltActivityAndUser)
		{
			if(theRltActivityAndUser == null)
			{
				throw new ArgumentNullException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.RLT_ACTIVITYANDUSERs.Count(au => au.userID == theRltActivityAndUser.userID && au.ActivityID == theRltActivityAndUser.ActivityID) > 0;

					if(!isExist)
					{
						db.RLT_ACTIVITYANDUSERs.InsertOnSubmit(theRltActivityAndUser);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteActivity(List<int> activityIDs)
		{
			if(activityIDs == null || activityIDs.Count <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var tagSubmit = false;

					var dbRlt_UserAndActivity = db.RLT_ACTIVITYANDUSERs.Where(au => activityIDs.Contains(au.ActivityID));

					if(dbRlt_UserAndActivity != null)
					{
						db.RLT_ACTIVITYANDUSERs.DeleteAllOnSubmit(dbRlt_UserAndActivity);
						tagSubmit = true;
					}

					var dbRlt_ActivityAndShareTo = db.RLT_ActivityAndShareTos.Where(acs => activityIDs.Contains(acs.ActivityID));

					if(dbRlt_ActivityAndShareTo != null)
					{
						db.RLT_ActivityAndShareTos.DeleteAllOnSubmit(dbRlt_ActivityAndShareTo);
						tagSubmit = true;
					}

					var dbactivity = db.Activities.Where(ac => activityIDs.Contains(ac.ActivityID));

					if(dbactivity != null)
					{
						db.Activities.DeleteAllOnSubmit(dbactivity);
						tagSubmit = true;
					}

					if(tagSubmit)
					{
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyRegistrationActivity(int userID, int theActCatID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(userID <= 0 || theActCatID <= 0 || pageIndex < 0 || pageSize < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					totalRecords = (from ac in db.Activities
													where ac.ActivityCategoryID == theActCatID &&
													(from au in db.RLT_ACTIVITYANDUSERs
													 where au.userID == userID
													 select au.ActivityID).Contains(ac.ActivityID)
													select ac).Count();

					var data = (from ac in db.Activities
											where ac.ActivityCategoryID == theActCatID &&
											(from au in db.RLT_ACTIVITYANDUSERs
											 where au.userID == userID
											 select au.ActivityID).Contains(ac.ActivityID)
											orderby ac.DeadlineTime descending
											select ac).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyInitiatActivity(int userID, int theActCatID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(userID <= 0 || theActCatID <= 0 || pageSize < 0 || pageIndex < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					totalRecords = db.Activities.Count(ac => ac.ActivityInitiator == userID && ac.ActivityCategoryID == theActCatID);

					var data = db.Activities.Where(ac => ac.ActivityInitiator == userID && ac.ActivityCategoryID == theActCatID).OrderByDescending(a => a.DeadlineTime).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyRegistrationActivity(int userID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(userID <= 0 || pageSize < 0 || pageIndex < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					totalRecords = (from ac in db.Activities
													where (from au in db.RLT_ACTIVITYANDUSERs
																 where au.userID == userID
																 select au.ActivityID).Contains(ac.ActivityID)
													select ac).Count();

					var data = (from ac in db.Activities
											where (from au in db.RLT_ACTIVITYANDUSERs
														 where au.userID == userID
														 select au.ActivityID).Contains(ac.ActivityID)
											orderby ac.DeadlineTime descending
											select ac).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<Activity> GetMyInitiatActivity(int userID, int pageIndex, int pageSize, out int totalRecords)
		{
			if(userID <= 0 || pageIndex < 0 || pageSize < 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					totalRecords = db.Activities.Count(ac => ac.ActivityInitiator == userID);

					var data = db.Activities.Where(ac => ac.ActivityInitiator == userID).OrderByDescending(a => a.DeadlineTime).Skip(pageIndex * pageSize).Take(pageSize);

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		#endregion

		#region IActivityProvider Members


		public List<RLT_ActivityAndShareTo> GetActivityAndShareToByActivity(int activityID)
		{
			if(activityID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<RLT_ActivityAndShareTo> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<RLT_ActivityAndShareTo>(acs => acs.User);
					db.LoadOptions = dlo;

					var data = from acs in db.RLT_ActivityAndShareTos
										 where acs.ActivityID == activityID
										 select acs;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public void CreateActivityAndShareTo(List<RLT_ActivityAndShareTo> lstActivityAndShareTo)
		{
			if(lstActivityAndShareTo == null || lstActivityAndShareTo.Count <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					db.RLT_ActivityAndShareTos.InsertAllOnSubmit(lstActivityAndShareTo);
					db.SubmitChanges();
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}
		}

		public void SaveActivityAndShareTo(int activityID, List<RLT_ActivityAndShareTo> lstActivityAndShareTo)
		{
			if(activityID <= 0 || lstActivityAndShareTo == null)
			{
				throw new ArgumentOutOfRangeException();
			}

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbActivityAndShareTo = from acs in db.RLT_ActivityAndShareTos
																		 where acs.ActivityID == activityID
																		 select acs;

					if(dbActivityAndShareTo != null)
					{
						var lstDBActivityAndShareTo = dbActivityAndShareTo.ToList();

						var deleteItems = lstDBActivityAndShareTo.Except(lstActivityAndShareTo);

						if(deleteItems != null)
						{
							db.RLT_ActivityAndShareTos.DeleteAllOnSubmit(deleteItems);
						}

						var newItems = lstActivityAndShareTo.Except(lstDBActivityAndShareTo);

						if(newItems != null)
						{
							db.RLT_ActivityAndShareTos.InsertAllOnSubmit(newItems);
						}

						db.SubmitChanges();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}
		}

		public List<Activity> GetNewActivities(int count, int drugStoreID, int userID)
		{
			if(count <= 0 || drugStoreID <= 0 || userID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			List<Activity> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					DataLoadOptions dlo = new DataLoadOptions();
					dlo.LoadWith<Activity>(q => q.RLT_ACTIVITYANDUSERs);
					db.LoadOptions = dlo;

					var user = db.Users.SingleOrDefault(u => u.UserID == userID);

					var data = from act in db.Activities
										 //where ac.DrugStoreID == drugStoreID
										 where (act.DrugStoreID == drugStoreID) || (act.IsPublic == 1) || ((from acs in db.RLT_ActivityAndShareTos
																																														 where acs.ShareToDrugStoreID == user.DrugStoreID
																																														 select acs.ActivityID).Contains(act.ActivityID)) || ((from acs in db.RLT_ActivityAndShareTos
																																																																									 where acs.ShareToMemberID == userID
																																																																									 select acs.ActivityID).Contains(act.ActivityID))
										 orderby act.ActivityStartTime descending
										 select act;

					if(data != null)
					{
						result = data.Take(count).ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		#endregion
	}
}
